package com.hotel.controller;

import com.hotel.pojo.Food;
import com.hotel.pojo.FoodType;
import com.hotel.pojo.Table;
import com.hotel.utils.Condition;
import com.hotel.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by LinYaTing on 2018/12/1 12:59
 */
@WebServlet(name = "AppServlet", urlPatterns = "/app")
public class AppServlet extends BaseServlet {

    private Object uri;

    //进入主页，展示菜品菜系
    public Object foodDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        //获取餐桌id，根据id查询，再把查询到的结果保存到session（生成订单用)
        Object obj = session.getAttribute("dinnerTable");
        if (obj == null) {
            String tableId = request.getParameter("tableId");
            Table table = tableService.findById(Integer.valueOf(tableId));
            // 保存到session
            session.setAttribute("dinnerTable", table);
        }
        //1.2 查询所有的"菜品信息",保存
        PageBean<Food> pb = new PageBean<Food>();
        //获取当前页
        String currentPage = request.getParameter("currentPage");
        //判断
        if (currentPage == null || "".equals(currentPage)) {
            //第一次访问，设置当前也为1
            pb.setCurrentPage(1);
        }else {
            //设置当前页
            pb.setCurrentPage(Integer.parseInt(currentPage));
        }
        //条件对象
        Condition condition = new Condition();
        //分页参数：菜系id
        String foodType_id = request.getParameter("foodType_id");
        if (foodType_id != null) {
            condition.setFoodType_id(Integer.parseInt(foodType_id));
        }
        //分页参数，菜名称
        String foodName = request.getParameter("foodName");
        condition.setFoodName(foodName);
        //设置条件对象到pb中
        pb.setCondition(condition);

        //  ---->分页查询
        foodService.getAll(pb);
        request.setAttribute("pb", pb);

        //查询所有的”菜系信息" ,保存
        List<FoodType> listFoodType = foodTypeService.findAll();
        request.setAttribute("listFoodType", listFoodType);
        //跳转
        uri = request.getRequestDispatcher("/app/menu.jsp");
        return uri;
    }
}
